//
//  AppManager.swift
//  YFWatchDemo
//
//  Created by FYWCQ on 2019/7/9.
//  Copyright © 2019 YFWCQ. All rights reserved.
//

import Foundation
import HandyJSON

let kUserDefaultsKey:String = "kUserDefaultsKey"

struct UserData:HandyJSON {
    var userToken:String?
    var loginUser:User?
    
    func isValid() -> Bool {
        guard  userToken?.count ?? 0 > 0 else {
            return false
        }
        
        guard loginUser?.id?.intValue ?? 0 > 0 else {
            return false
        }
        
        return true
    }
}

struct AppManager:HandyJSON {
    var userData:UserData?
    
    mutating func saveToUserDefaults() -> Void {
        self.userData = UserData.deserialize(from: "")
        let jsonString = self.toJSONString()
        let userDefaults = UserDefaults.standard
        userDefaults.set(jsonString, forKey: kUserDefaultsKey)
    }
}


struct User:HandyJSON {
    var id:NSNumber?
    var username:String?
}


var shareAppManager:AppManager?

func userDataSetting() -> Void {
    
}

func readUserDataFromUserDefaults() -> Void {
    if let jsonString:String = UserDefaults.standard.value(forKey: kUserDefaultsKey) as? String {
        shareAppManager = AppManager.deserialize(from: jsonString) ?? AppManager()
        if shareAppManager?.userData?.userToken?.count == 0 {
            
        }
    } else {
        shareAppManager = AppManager()

    }
}
